import rosbag
from colorama import Fore, Style
import os
import sys
import warnings
import def_function
from datetime import datetime

# 获取当前脚本的完整路径
script_path = os.path.abspath(sys.argv[0])
# 从完整路径中获取目录
script_dir = os.path.dirname(script_path)
# 从完整路径中分离出文件名
script_name = os.path.basename(script_path)
# 使用 splitext() 函数分离文件名和扩展名
script_name_without_extension, extension = os.path.splitext(script_name)

print(Fore.YELLOW+f"python script name:", sys.argv[0])
if len(sys.argv)!=2:
    print(Fore.RED+f"you need python params!")
    sys.exit()

current_rosbag = script_dir + "/" + sys.argv[1]
if os.path.isabs(sys.argv[1]):
    current_rosbag = sys.argv[1]

#print(Fore.WHITE+f"current_rosbag:", current_rosbag)
rosbag_data = rosbag.Bag(current_rosbag)
# 获取类型和主题信息
# rosbag_info = rosbag_data.get_type_and_topic_info()
# info.topics 是一个字典，它的键是 topic 的名字
# all_topics = list(rosbag_info.topics.keys())
topics = ['/msd/function_module_status']

error_msg = ""
error_msg_arry = []
record_time = 0.0
start_time = 0.0
end_time = 0.0

status = 0
status_last = 0
message = ""
for msg_topic, msg, t in rosbag_data.read_messages(topics=topics):
    if msg_topic == '/msd/function_module_status':
        message = standstill = msg.message
        status = msg.status

    if 2 == status_last and 0 == status:
        # quit auto
        error_msg = message
        error_msg = "[" + str(t.to_sec()) + "-" + str(t.to_sec()) + "]" + message
        error_msg_arry.append(error_msg)
    status_last = status

if error_msg_arry:
    print(Fore.RED+f"", script_name_without_extension)
    # 打印数组中的每个元素, 每个元素一行
    for item in error_msg_arry:
        print(Fore.CYAN+f"", item)

rosbag_data.close()
